package com.bbs.action;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import util.VerifyCode;

import com.opensymphony.xwork2.ActionSupport;

public class VerifyCodeAction extends ActionSupport {

	public String verifyCode() throws IOException {

		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("Cache-Control", "no-cache");

		// �����֤�룬д���û�session
		String verifyCode = VerifyCode.generateTextCode(
				VerifyCode.TYPE_ALL_MIXED, 4, "0oOilJI1");
		request.getSession().setAttribute("verifyCode", verifyCode);

		// �����֤���ͻ���
		ServletOutputStream outputStream = response.getOutputStream();
		response.setContentType("image/jpeg");
		BufferedImage bim = VerifyCode.generateImageCode(verifyCode, 90, 30, 2,
				true, Color.WHITE, Color.BLACK, null);
		ImageIO.write(bim, "JPEG", outputStream);
		outputStream.flush();
		outputStream.close();
		
		return "success";
	}
}
